#include "Game.h"
#include <assert.h>
#include "InputManager.h"
#include "EffectManager.h"
#include "D3DApp.h"
struct PosVertex
{
	D3DXVECTOR3 pos;
};
Game::Game()
{

	//Vertex Declaration from MSDN. Not Made by me.
	// Create the vertex element array.
	/*
	D3DVERTEXELEMENT9 elements[] =
	{
		{0, 0, D3DDECLTYPE_FLOAT3,
			D3DDECLMETHOD_DEFAULT,
			D3DDECLUSAGE_POSITION, 0},

			/*
			{0, 12, D3DDECLTYPE_FLOAT3,
			D3DDECLMETHOD_DEFAULT,
			D3DDECLUSAGE_NORMAL, 0},

			{0, 24, D3DDECLTYPE_FLOAT2,
			D3DDECLMETHOD_DEFAULT,
			D3DDECLUSAGE_TEXCOORD, 0},
			

			D3DDECL_END() 
	};
	*/
	// Use the vertex element array to create a vertex declaration.
	//TheD3DApp::Instance()->GetD3DDev()->CreateVertexDeclaration(elements,&m_VertexDec);
	//TheD3DApp::Instance()->GetD3DDev()->SetVertexDeclaration(m_VertexDec);
	m_GameTime=0;

}


void Game::Init()
{
	D3DXMatrixPerspectiveFovLH(&m_matProjection,
		D3DXToRadian(45),    // the horizontal field of view
		800/600, // aspect ratio
		1.0f,    // the near view-plane
		100.0f);    // the far view-plane
	//TheD3DApp::Instance()->GetD3DDev()->SetTransform(D3DTS_PROJECTION, &m_matProjection);     // set the projection

	TheD3DApp::Instance()->GetD3DDev()->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
	TheD3DApp::Instance()->GetD3DDev()->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
	TheD3DApp::Instance()->GetD3DDev()->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);

	//TheD3DApp::Instance()->GetDeviceClass()->SetRenderState(D3DRS_LIGHTING, false);

	TheD3DApp::Instance()->GetD3DDev()->SetRenderState(D3DRS_AMBIENT, RGB(50,50,50));

	D3DLIGHT9 Light;
	D3DMATERIAL9 material;    // create the material struct

	ZeroMemory(&Light, sizeof(Light));
	Light.Type = D3DLIGHT_POINT;
	Light.Diffuse = D3DXCOLOR(10.5f, 0.5f, 0.5f, 1.0f);
	Light.Position = D3DXVECTOR3(0.0f, 5.0f, 0.0f);
	Light.Range = 100.0f;
	Light.Attenuation0 = 0.0f;
	Light.Attenuation1 = 0.125f;
	Light.Attenuation2 = 0.0f;

	TheD3DApp::Instance()->GetD3DDev()->SetLight(0, &Light);
	TheD3DApp::Instance()->GetD3DDev()->LightEnable(0, true);

	ZeroMemory(&material, sizeof(D3DMATERIAL9));    // clear out the struct for use
	material.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);    // set diffuse color to white
	material.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);    // set ambient color to white

	TheD3DApp::Instance()->GetD3DDev()->SetMaterial(&material);    // set the globably-used material to &material

	TheD3DApp::Instance()->GetD3DDev()->SetRenderState(D3DRS_LIGHTING, true);

	TheD3DApp::Instance()->GetD3DDev()->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

	TheInputManager::Instance()->InitInputManager();
	TheInputManager::Instance()->InitKeyboard();
	//InputManagerObj::Instance()->InitMouse();

	TheEffectManager::Instance()->Init();
}
void Game::Clean()
{
	while(!m_States.empty())
	{
		m_States.back()->Clean();
		m_States.pop_back();
	}
	TheInputManager::Instance()->Clean();
	TheEffectManager::Instance()->Clean();
}

void Game::ChangeState(GameState* state)
{
	
	if(!m_States.empty())
	{
		m_States.back()->Clean();
		m_States.pop_back();
	}

	m_States.push_back(state);
	m_States.back()->Init();
	

}
void Game::PushState(GameState* state)
{
	
	if(!m_States.empty())
	{
		m_States.back()->Pause();
	}

	m_States.push_back(state);
	m_States.back()->Init();
	
}
void Game::PopState()
{
	
	if(!m_States.empty())
	{
		m_States.back()->Clean();
		m_States.pop_back();
	}

	if(!m_States.empty())
	{
		m_States.back()->Resume();
	}
	
}

void Game::HandleInput()
{
	if(m_States.empty())
	{
		assert("No States!");
	}
	m_States.back()->HandleInput();
}
void Game::HandleCollisions()
{
	if(m_States.empty())
	{
		assert("No States!");
	}
	m_States.back()->HandleCollisions();
}

void Game::Update(float dt)
{
	if(m_States.empty())
	{
		MessageBox( NULL, L"No States Loaded", L"DxFramework.exe", MB_OK );
	}
	m_States.back()->Update(dt);
	m_GameTime+=dt;

	m_lastFPSUpdate+=dt;
	m_frames++;
	if(m_lastFPSUpdate>1)
	{
		m_FPS=m_frames;
		m_frames=0;
		m_lastFPSUpdate=0;
	}
}
void Game::Render()
{

	if(m_States.empty())
	{
		MessageBox( NULL, L"No States Loaded", L"DxFramework.exe", MB_OK );
		
	}
	m_States.back()->Render();
}
D3DMATRIX Game::GetProj()
{
	return m_matProjection;
}
float Game::GetTime()
{
	return m_GameTime;
}
void Game::SetSock(Socket* s)
{
	m_Sock=s;
}
Socket* Game::GetSock()
{
	return m_Sock;
}